/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_sequence.cpp

	$Header: /heroes4/script_sequence.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_sequence.h"

#include "adventure_map.h"
#include "streambuf_operators.h"
#include "sized_int_types.h"

// --------------------------------------------------------------------------
// t_script_sequence members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
t_script_sequence::t_script_sequence( t_script_sequence const & other )
	:	t_abstract_script_action( other )
{
	int count = other.m_subaction_ptrs.size();
	m_subaction_ptrs.reserve( count );

	int num;
	for ( num = 0; num < count; ++num )
		m_subaction_ptrs.push_back( other.m_subaction_ptrs[ num ]->clone() );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_sequence::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	int count = get< t_uint16 >( stream );

	t_subaction_ptr_list new_subaction_ptrs;
	new_subaction_ptrs.reserve( count );

	while ( count > 0 )
	{
		new_subaction_ptrs.push_back( t_subaction::reconstruct_from_map( stream, format_version ) );
		if ( new_subaction_ptrs.back().get() == 0 )
			return false;

		--count;
	}

	m_subaction_ptrs.swap( new_subaction_ptrs );

	return true;
}

bool t_script_sequence::read( std::streambuf& stream, int version )
{
	int count = get< t_uint16 >( stream );

	t_subaction_ptr_list new_subaction_ptrs;
	new_subaction_ptrs.reserve( count );

	while ( count > 0 )
	{
		new_subaction_ptrs.push_back( t_subaction::reconstruct( stream, version ) );
		if ( new_subaction_ptrs.back().get() == 0 )
			return false;

		--count;
	}

	m_subaction_ptrs.swap( new_subaction_ptrs );

	return true;
}

bool t_script_sequence::write( std::streambuf& stream ) const
{
	if (t_abstract_script_action::write( stream ) == false) 
		return false;
	
	int size = m_subaction_ptrs.size();
	put< t_uint16 >( stream, size );
	for (int i = 0; i < size; i++) {
		if (m_subaction_ptrs[i]->write( stream ) == false)
			return false;
	}
	return true;
}

void t_script_sequence::execute(t_script_context_hero const& context) const
{
	bool removal_requensted = false;
	int count = get_subaction_count();
	for (int loop = 0; loop < count; loop++) {
		const t_subaction& action = get_subaction(loop);
		action.execute(context);
		if( context.map->is_game_over() )
			return;
		executed_subaction(action);
	}
}

void t_script_sequence::execute(t_script_context_town const& context) const
{
	int count = get_subaction_count();
	for (int loop = 0; loop < count; loop++) {
		const t_subaction& action = get_subaction(loop);
		action.execute(context);
		if( context.map->is_game_over() )
			return;
		executed_subaction(action);
	}
}

void t_script_sequence::execute(t_script_context_object const& context)  const
{
	int count = get_subaction_count();
	for (int loop = 0; loop < count; loop++) {
		const t_subaction& action = get_subaction(loop);
		action.execute(context);
		if( context.map->is_game_over() )
			return;
		executed_subaction(action);
	}
}

void t_script_sequence::execute(t_script_context_army const& context)  const
{
	int count = get_subaction_count();
	for (int loop = 0; loop < count; loop++) {
		const t_subaction& action = get_subaction(loop);
		action.execute(context);
		if( context.map->is_game_over() )
			return;
		executed_subaction(action);
	}
}

void t_script_sequence::execute(t_script_context_global const& context) const
{
	int count = get_subaction_count();
	for (int loop = 0; loop < count; loop++) {
		const t_subaction& action = get_subaction(loop);
		action.execute(context);
		if( context.map->is_game_over() )
			return;
		executed_subaction(action);
	}
}

